package me.itsoo.artemis.framework.core.tuple;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;

import java.io.Serializable;

/**
 * ImmutablePair
 *
 * @param <K> Key type for Serializable
 * @param <V> Value type for Serializable
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/10
 */
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ImmutablePair<K extends Serializable, V extends Serializable> implements Pair<K, V>, Serializable {

    private static final long serialVersionUID = -1971492530684622261L;

    private static final Pair<Serializable, Serializable> EMPTY = new EmptyImmutablePair();

    private final K key;

    private final V value;

    public static <K extends Serializable, V extends Serializable> Pair<K, V> create(K key, V value) {
        return new ImmutablePair<>(key, value);
    }

    public static <K extends Serializable, V extends Serializable> Pair<K, V> empty() {
        return (ImmutablePair<K, V>) EMPTY;
    }

    @Override
    public K getKey() {
        return this.key;
    }

    @Override
    public V getValue() {
        return this.value;
    }

    /**
     * EmptyImmutablePair
     */
    private static class EmptyImmutablePair extends ImmutablePair<Serializable, Serializable> {

        private static final long serialVersionUID = -1971492530684622262L;

        private EmptyImmutablePair() {
            super(null, null);
        }

        @Override
        public Serializable getKey() {
            return null;
        }

        @Override
        public Serializable getValue() {
            return null;
        }
    }
}
